package com.nginxmanager.controller;

import com.nginxmanager.comment.StringTool;
import com.nginxmanager.domain.Proxy;
import com.nginxmanager.server.serverIpm.HttpDoctor;
import com.nginxmanager.server.serverIpm.NginxConfImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018-01-09.
 */
@RestController
@RequestMapping("/nginx")
@Api(tags = "负载均衡配置API")
public class PoxyController {
    @Autowired
    private NginxConfImpl nginxConf;
    private StringTool stringTool = new StringTool();
    private HttpDoctor httpDoctor = new HttpDoctor();
    Proxy proxy ;

    @GetMapping("/readUpStream")
    @ApiOperation("查看反向代理的主机信息")
    public ResponseEntity<List<Proxy>> find(@RequestParam("address")String address){
        List<String> list = new ArrayList<String>();
        list= nginxConf.findUptStream(address);
       List<Proxy> proxyList = new ArrayList<Proxy>();
        for(int i = 0 ; i < list.size() ; i++) {
            proxyList.add(stringTool.getProxy(list.get(i)));
            boolean state = httpDoctor.doctor(proxyList.get(i));
            proxyList.get(i).setState(state);
        }
        return ResponseEntity.status(200).body(proxyList);
    }

    @DeleteMapping("/delUpstream")
    @ApiOperation("删除一台主机")
    public ResponseEntity<String> delete(@RequestParam("address")String address,@RequestBody Proxy proxy){
        int type = nginxConf.deleteUptStream(address,proxy);
        if(type==0){
            ResponseEntity.status(400).body(null);
        }
        return ResponseEntity.status(200).body(null);
    }

    @PostMapping("/addUpstream")
    @ApiOperation("添加一台代理主机")
    public ResponseEntity<String> add(@RequestParam("address")String address,@RequestBody Proxy proxy){;
        try {
            nginxConf.addUptStream(address,proxy);
        }catch (Exception e){
            ResponseEntity.status(415).body(e);
        }
        return  ResponseEntity.status(200).body(null);
    }

    @PutMapping("/putUpstream")
    @ApiOperation("修改一台代理主机信息")
    public ResponseEntity<String> upt(@RequestParam("address")String address,@RequestBody Proxy newProxy){
        try {
            nginxConf.gaiUptStream(address, newProxy);
        }catch (Exception e){
            ResponseEntity.status(415).body(e);
        }
        return  ResponseEntity.status(200).body(null);
    }
    @GetMapping("/readConf")
    @ApiOperation("查看配置文件")
    public ResponseEntity<List<String>> read(@RequestParam("address")String address){
        List<String> list = new ArrayList<String>();
        list= nginxConf.readUptStream(address);
        return new ResponseEntity<List<String>>(list, HttpStatus.valueOf(200));
    }
    @GetMapping("/init")
    @ApiOperation("初始化配置文件")
    public ResponseEntity<List<String>> ini(@RequestParam("address")String address){

        List<String> list = new ArrayList<String>();
        int type=nginxConf.seeUptStream(address);
        if (type!=3){
            try {
                nginxConf.initProxy(address);
                list.add("proxy初始化成功");
            }catch (Exception e){
                list.add("proxy初始化失败");
            }
            try {
                nginxConf.initUptStream(address);
                list.add("initUptStream初始化成功");
            }catch (Exception e){
                list.add("initUptStream初始化失败");
            }
        }
        nginxConf.initProxy(address);
        return new ResponseEntity<List<String>>(list, HttpStatus.valueOf(200));
    }
}
